

#ifndef Lib_SensingADC_h
#define Lib_SensingADC_h




#define VDC_SCALE				138

#define VDC_HIGH_LIMIT_VOLTAGE	466
#define VDC_LOW_LIMIT_VOLTAGE	197

typedef struct
{
	UINT8	ucMotorType;
	UINT8	ucSenseSector;
	INT16	snUalfa;
	INT16	snUbeta;

        //PWM State Register Flag
	UINT8	gucPWM_Normal_F;
	UINT8	gucPWM_DBRAK_F;


	UINT16	unLowCurrentCnt;
	UINT16	unStopChkCnt;

	UINT8	ucDBrakingSTOP_F;
	UINT8	ucSSLSHighSpeed_F;
	UINT8	ucDirection_F;


	UINT16	unBootStrap_Sntr;

        //flux weakening
	struct
	{
		INT16	snVfwErr;
		INT32	slIdeRef_fw;
		INT16	snKifw;
		INT16	snIdeRef_fw;
		INT16	snIqeRef_fw_Limit;
		INT32	slKLimit_Vfw;
		UINT16	unT12sum_Cntr;
		INT32	slT12sum;
	}stFW;

        //Timer
	struct
	{
		UINT32	ulMotorBaseTimer;
		UINT32	ulSWTimer_PWMPeriod_No_3;
		UINT32	ulSWTimer_PWMPeriod_No_4;
		UINT32	ulSWTimer_PWMPeriod_No_5;
		UINT32	ulSWTimer_PWMPeriod_No_6;
	}stPWM0Timer;

        //Speed controller
	struct
	{
		UINT8	ulSpeed_Cntl_Cntr;
		INT32	slIqeRef_Temp;
		INT16	snWeRef_Abs;
		INT32	slWeFdb;
		INT16	snWeRef;
		INT16	snWeFdb;
		INT16	snWeFdb0ld;
		INT16	snWeFdb_LPF;
		INT32	slKpWe;
		INT32	slKiWe;
		INT32	slKcWe;
	}stSpeedCtrl;

        //VC DPWM
	struct
	{
		INT16	snVun_duty;
		INT16	snVvn_duty;
		INT16	snVwn_duty;
		INT32	slVdeEst;
		INT32	slVqeEst;
	}stDPwm;

        //VC_Clac_SIN
	struct
	{
		INT16	snSin;
		INT16	snCos;
	}stClacSin;

	//Detect current voltage
	struct
	{
		UINT16	unICalib_Cntr;
		UINT8	ucTb0_ADAB_Err_CNT;
		INT16	snIas;
		INT16	snIbs;
		INT16	snIcs;
		UINT16	unVdc;
		UINT16	unVdc0;
		INT16	snIa0;
		INT16	snIb0;
		INT16	snIc0;

		UINT16	unVdc0Reg;
		INT16	snIa0Reg;
		INT16	snIb0Reg;
		INT16	snIc0Reg;
		UINT32	ulIa0_Temp;
		UINT32	ulIb0_Temp;
		UINT32	ulIc0_Temp;
		UINT32	gulIb0_ADAResult_Temp;
		UINT32	gulIb0_ADBResult_Temp;
		UINT32	ulVdc0_Temp;
		UINT16	unVdcAvg;

		INT16	snVunDuty;
		INT16	snVvnDuty;
		INT16	snVwnDuty;

		INT16	snVds;
		INT16	snVqs;
	}stDetect_CurVol;


	//stSenorless
	struct
	{
                //BackObs()
		UINT16	unRpm_720;
		INT32	slWrpm_Integ;
		INT16	snWrpm_Old;
		INT16	snWrpm_LPF;
		INT16	gnWrpm_LPF_Abs;

		INT32	slEdse_Integ;
		INT16	snEdse_Old;
		INT16	snEdse_LPF;

		INT32	slEqse_Integ;
		INT16	snEqse_Old;
		INT16	snEqse_LPF;

		INT16	snIdss;
		INT16	snIqss;

		INT32	glIdqSq;
		INT32	glEdqSq;

		INT16	gnVdss_d;
		INT16	gnVqss_d;

		INT16	snIdsReg;
		INT16	snIqsReg;

		INT16	gnVdss;
		INT16	gnVqss;

		INT32	glIdqsq_Integ;
		INT16	gnIdqsq_Old;
		INT16	gnIdqsq_LPF;

		INT32	slIdso;
		INT32	slIqso;

		INT16	snIdso;
		INT16	snIqso;

		INT32	slEdso;
		INT32	slEqso;

		INT16	snEdss;
		INT16	snEqss;

		INT16	snEdse;
		INT16	snEqse;

		INT32	slLsWbo2;
		INT32	slLsWboSqTs;
		INT32	slLsWbo2_Rs;
		UINT16	gunInvLsTs;

		UINT16	unangle;
		UINT16	unangleSR;
		INT32	slDeltaAngleSR;
		INT32	slAngleSR;
		INT32	slSampleCompCoff;
		INT32	slTlEstPPInvJmInvTs;
		INT16	snWrpmSR;
		INT32	slWeEstInvTs;
		INT16	gnWrpmSR_Ads;
		INT32	lEqse_LPF_Abs;
		INT32	glWe_Sq;
		INT32	glWe_Sq_Limit_Integ;
		INT32	glWe_Sq_Limit_Old;
		INT32	glWe_Sq_Limit_LPF;
		INT32	glWe_Sq_Limit_Abs_LPF;

		UINT8	ucSpeedRefCalc_F;
		INT16	nWe_Ref_Buf;

		INT16	nPWM_Angle_Comp;
	}stSensorless;

        //stParameter
	struct
	{
		UINT16	sunLs;
		UINT16	sunRs;
		UINT16	sunRsBitRl;
		UINT16	sunLs_m;
		UINT16	sunRs_m;

		INT32	lLs;
		INT32	lKE;
	}stParameter;

        //coordinate transformation & current control
	struct
	{
		UINT16	snIds;
		UINT16	snIqs;
		UINT16	snIdsOld;
		UINT16	snIqsOld;
		INT32	slIds;
		INT32	slIqs;
		UINT16	snIde_LPF;
		UINT16	snIqe_LPF;
		UINT16	snIdeRef;
		UINT16	snIqeRef;
		UINT16	snVde;
		UINT16	snVqe;
		INT32	slVdeRef;
		INT32	slVdeRefOld;
		INT32	slVqeRef;
		INT32	slVqeRefOld;
		UINT16	snVdeRef;
		UINT16	snVqeRef;
		INT32	slVdeRef_Temp;
		INT32	slVqeRef_Temp;

		INT32	slKpIde;
		INT32	slKiIde;
		INT32	slKcIde;
		INT32	slKpIqe;
		INT32	slKiIqe;
		INT32	slKcIqe;
	}stCoordinateTrans;

        //Power Detection
	struct
	{
		INT32	glMotorPower_Integ;
		INT32	glMotorPower_Old;
		INT32	glMotorPower_LPF;
	}PowerDetect;

	struct
	{
		UINT8	sucPwmFrqChange_F;
		UINT8	sucPwmFrqUpDown_F;
		UINT8	sucTsRatioApply_F;
		UINT8	sucPwmFrqChange_66usCnt;
		UINT16	sucPWMPeriod_Sweep;
		INT16	snFreqN;
		INT16	snTsRatio;
		INT16	snInvTsRatio;
	}stPwmFrqChange;

	struct
	{
		UINT8	ucMotorAlignFinish_F;
		UINT16	unAngle_Ls;
		UINT16	unLs_temp;
		UINT8	ucLsDetectionF;
		UINT8	ucLsReCalc_F;
		UINT8	ucLsDetectFinish_F;
		UINT8	ucAlign_Start_F;
		UINT8	ucStartup_F;
		UINT32	lVqeLsSum;
		UINT32	lIdeLsSum;
		UINT32	lIqeLsSum;
	}Align;

	struct
	{
		UINT8	ucMOTOR_OPEN_LOOP_F;
		INT32	lVdeRef_for_Openloop;
		INT32	lVqeRef_for_Openloop;
		UINT8	ucRpmOpenLoopStbTimeFlag;
		UINT8	ucRotation_Cntr;
		UINT16	unAngle_Pre;
		UINT8	ucRpmOpenLoopStb100msCnt;
	}OpenLoop;

	struct
	{
		UINT8	ucMttr_Start_Sequence;
		UINT8	ucSwingStartInfo_F;
		UINT8	ucSwingDeceleration_F;
		INT16	nIqeRef_Max;
		INT16	nIqeRef_Min;
		UINT8	ucSwingAngle_CNT_Max;
		UINT8	ucSwingStartAngle;
		UINT8	ucSwing10msCnt_F;
		INT32	lStartingIqeRef_integ;
		UINT8	ucSwingStartDirectionChangeF;
	}SwingStart;

	struct
	{
		UINT8	ucMotorLockChangeOnOff_F;

		UINT16	unAngleErrCnt;
		UINT16	unAngleErrCnt2;
		UINT16	unSpeedErrCnt;
		UINT8	ucLE_Count_ResetCnt;
		INT16	nWeCheck;
		INT32	lWeCheck;
		UINT16	unWeCheckCnt;
	}Protection;

	struct
	{
		UINT8	ucCordOff_DetectCNT;
		UINT8	ucCordOff_2ndProtection_F;
		UINT8	ucCurrentOffset_Err_CNT;
		UINT8	ucCordOffMotorStopF;
	}CordOffDetection;

	struct
	{
		UINT8	ucPos_Data;
		UINT8	ucPos_Data_Pre;
		UINT16	unSame_Pos_Cntr;
		UINT8	ucConf_Pos_Data;
	}DetPosition;

	struct
	{
		UINT8	ucMotionDirectionChangeStateF;
		UINT16	unIdqStopCnt;
		INT32	lAngleInteg;
		UINT8	ucAngle_3P75deg_CNT;
	}Motion;

	struct
	{
		UINT8	ucMotor_wise_F;
		UINT8	ucMotor_wise_Pre_F;
		UINT8	ucFWeakening_F;
		UINT8	ucBStrap_F;
		UINT8	ucICalib_F;
		UINT8	ucCordoff_F;
	}Mtr_Status_Flag;

	struct
	{
		UINT8	ucTIME_SLOPE_CONTROL_100MS_F;
	}TimeFlag;

}T_Pwm_VectorControl;


void VC_Get_Vdc(void);
UINT8 Is_DC_Link_Valid(void);
UINT16 GetVdcAvg(void);

#endif